package com.scaffold.upload.dao;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.LogicalExpression;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import wangjg.commons.page.Page;

import com.scaffold.authority.dao.BasicBizDAO;
import com.scaffold.upload.entity.FileInfo;

@Repository
public class FileInfoDAO extends BasicBizDAO<FileInfo, Long> {

	public List<FileInfo> pageQuery(Page page) {
		DetachedCriteria criteria = DetachedCriteria
				.forClass(this.persistentClass);
		criteria.addOrder(Order.desc("id"));
		return this.findByCriteria(criteria, page);
	}

	public List<FileInfo> pageQuery(Page page, String contentType, String temp, String keyword) {
		DetachedCriteria criteria = DetachedCriteria.forClass(this.persistentClass);
		if (contentType != null) {
			criteria.add(Restrictions.eq("contentType", contentType));
		}
		if (temp != null) {
			criteria.add(Restrictions.eq("temp", temp));
		}
		if (keyword != null && !keyword.isEmpty()) {
			LogicalExpression expr = Restrictions.or(
					Restrictions.like("fileName", keyword, MatchMode.ANYWHERE).ignoreCase(),
					Restrictions.like("filePath", keyword, MatchMode.ANYWHERE).ignoreCase());
			criteria.add(expr);
		}
		criteria.addOrder(Order.desc("id"));
		return this.findByCriteria(criteria, page);
	}

}
